package com.example.hasee.addinfo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.hasee.libraryreservation.R;

import static cn.bmob.v3.Bmob.getApplicationContext;

public class AddInfoFragment extends Fragment implements AddInfoContract.View{
    private AddInfoContract.Presenter mPresenter;
    private EditText edtName,edtNo,edtSubject,edtYear,edtGroup;
    private Button btnAdd;
    public AddInfoFragment(){

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_add_info, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenter=new AddInfoPresenter(this);
        edtName=view.findViewById(R.id.edtName);
        edtNo=view.findViewById(R.id.edtNo);
        edtSubject=view.findViewById(R.id.edtSubject);
        edtYear=view.findViewById(R.id.edtYear);
        edtGroup=view.findViewById(R.id.edtGroup);
        btnAdd=view.findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=edtName.getText().toString().trim();
                String no=edtNo.getText().toString().trim();
                String subject=edtSubject.getText().toString().trim();
                String year=edtYear.getText().toString().trim();
                String group=edtGroup.getText().toString().trim();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(no) || TextUtils.isEmpty(subject) || TextUtils.isEmpty(year) ){
                    showError("添加不能为空");
                    return;
                }
                mPresenter.addInfo(name,no,subject,year,group);
                edtName.setText("");
                edtNo.setText("");
                edtSubject.setText("");
                edtYear.setText("");
                edtGroup.setText("");
            }
        });
    }

    @Override
    public void showError(String message) {
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
    }
}
